package com.msb.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.msb.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AliPayTemplate {
    // 测试支付账号 sbjxwm8845@sandbox.com
    // 商户appid
//    public static String APPID = "2021000117668159";
    public String APPID;
    // 私钥 pkcs8格式的
//    public static String RSA_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC901VZ+bxNJ2tTARSiknqGAF/KyuC5lDu7EMfIQNagoMqR2J8Reg8HB4x/vxYPq/+Ewfbm3jNW8DnhgNqt4maaF/lj1v+bilRoUOpLFJRsVDKJeODJbSlLt+uD44t18Ztf7dMDe2lqG+S+Rz7EBJ41WfgQJSHn0tTnNpg+1koDtFdJyhEjz+qDtqr9Jy+/Vgr2FU4JXQNH3oX+QU2PdG+LwSi/MMbicraLFbYGhUIllhw3mYCgS7H/TEBCwKinYcUL3lD/cv+3dpgPP7P5Q/9UW3mHe55rQynMa1BHnfIa1VZ70nz/4rVXib0VqSB8QCd4eG4lF6hfUqKN4FKxS7Z1AgMBAAECggEAIzlQ8DZAM8zpxGhB7PzjUXRGUjodQzJ3dvnhKI6nhPzxOekJc1PD8Bj9gEL0FAplirv3VdXLjwlapDal3XhSc6uZMmaV3cXobcb+AtqZxBRHoGzWwWnJLOy+/iU+Hq+rljxqWI4Q/IiahJhx1CvLM7kzJ8PMK9laemEOFTLsXw1IyC6AEdaunFR6xmBSSk0xUrefP0LnPSh9F3/f/0jJaJry4LrnVaxvrouoEIv/KoIlIlZLQLy71f121cL8LUeHrsRxYGQnzXzqVzQASVrlPchv9OXrMMm0/lh/fVvSmqlzgnf+ltilQlEeGv8UnAL2nt0VTYzdWVW5KqeRnC0RbQKBgQDuihZunQL9aXt8AoYrMIL+mslOYIgyeYKjJwPbarl7DmVnqHoXn8/02CRXTg7bTyzYifNh58DmftZHZVOsYEkptzfYMOre/QvAjEMelfFW6N3IEz/S6vzNo7/LQNdhZJFmCqaXgVNGcHwpV2tS9W7fW5RlV/dGCGbhwhwrsQv2HwKBgQDLuGmCZVBRAoFCfwmAHRODl0BAxbsk4dDRjujqglkjUdazz/dUn7+sT8k2jRirCCXd7d2+v8ixjUZGQ6x57Ms+2QloeYm+o3Ttx/E0rDKAKAIxalefUkfhWMgxxe6rA/Ep2X03Ab6bJv9sCepdiK3RlDFEQblSMHWa2rJmn0Y46wKBgQDLcdyV9LXzGf7mkmSJ+3MN4Ugl37Dpk7b2nbTtc4XETU/xa8DN21CK27mK+Jk1NL7j5Cw+6WEddToeZG5Ly0c7G/e2HGqFSr62NyqbsHjIjg1xsfEyW5bHcIyItsG7YAGeTSEugG9c2V0cCWIQvCEWwM287ZyJD2tnyp0qKnMwMwKBgQCekqQmKMCU3jNJVGNLwtY+tsv/3ynl8pQ9mlT4vY+u63QJMRcfWt2yvcQs3zVVZBk9n3rckKIieoo6BVFK8qPd/sjQ9tHef5Nzxw+c5XDFyt6yIkmkLULgSOBDxgMaQQ5GehF6cLQyRu/I5Ar21X4fRY/3xoT/UdMpP5RgRxg9EwKBgQDmEO3qnd6OyA1qdHpHyiLDkK2qBDT6pExqMZ+Rwjo8NbC3OtZd5WKPfQfCNVubaH1HgFdJSwYDWFknAVkYPBCkOQAa3AZMMFq/SuZ6+j5/U+96PEZOW+ZDohS3OO5cHKRsrX2aVXkVNq1FhVysyFl0HQ4xVBb0rRt3yLbEtgR/XA==";
    public String RSA_PRIVATE_KEY;
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
//    public static String notify_url = "http://localhost:8080/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";
    public String notify_url;
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
//    public static String return_url = "http://order.msb.com/detail";
    public String return_url;
    // 请求网关地址
//    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    public String URL;
    // 编码
//    public static String CHARSET = "UTF-8";
    public String CHARSET;
    // 返回格式
//    public static String FORMAT = "json";
    public String FORMAT;
    // 支付宝公钥
//    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqDcqmQKF6F4qjeUI4E4He+lKCLiK8b6+/escWWhKpqG+YQjNjIunTV3anUJTxM8GGMDyCz2xkWVNu21ltJnQji0mfDh8mojynL27basmlwW8WxOzxIEOUdKOfcxyFtxNiwngMQpruYz1DdrmRy6WHIMg1bzxRoLpSUpdkC6SSdhesPXTMsULi43ku/V1GfQ7DcaM1BS0fyCkPodzPG1mUo+PoshPKMnaFdlSRG/LadkKSZMnWkCH6gylja1iU0dC2j55B6vazd5RVsoxIQNxFi/Y2B8rkKnrfv3BPv/VlXBVRcf1SNmpzfJ/xlVax+SVHSRpL4t5mrGMtklOEx5oZQIDAQAB";
    public String ALIPAY_PUBLIC_KEY;
    // 日志记录目录
//    public static String log_path = "/log";
    public String log_path;
    // RSA2
//    public static String SIGNTYPE = "RSA2";
    public String SIGNTYPE;

    public String pay(PayVo payVo){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOutTraderNo());
        model.setSubject(payVo.getSubject());
        model.setTotalAmount(payVo.getTotalAmout());
        model.setBody(payVo.getBody());
        model.setTimeoutExpress("5000");
        model.setProductCode("11111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
